package org.znxs.comfriend;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.znxs.comfriend.activities.LoginActivity;
import org.znxs.comfriend.fragments.FriendsFragment;
import org.znxs.comfriend.fragments.HomeFragment;
import org.znxs.comfriend.fragments.MessagesFragment;
import org.znxs.comfriend.fragments.ProfileFragment;
import org.znxs.comfriend.models.User;
import org.znxs.comfriend.utils.LoginUser;
import org.znxs.comfriend.utils.UserUtils;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private BottomNavigationView bottomNav;
    private LoginUser loginUser;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(this);

        // 初始化实例
        loginUser = LoginUser.getInstance(this);

        // 默认加载首页
        loadFragment(new HomeFragment());

        // 初始化用户数据
        // todo 获取用户登录数据 保存到全局变量中
        // 初始化用户数据
        User currentUser = loginUser.getLoginUser();
        if (currentUser != null) {
            // 使用 Toast 显示欢迎信息
            Toast.makeText(this, "欢迎, " + currentUser.getUsername(), Toast.LENGTH_SHORT).show();
        } else {
            // 如果未登录，可以设置默认标题或其他操作
            Toast.makeText(this, "登录获取更多用户", Toast.LENGTH_SHORT).show();
        }

        // 初始化推荐用户列表
        fetchRecommendations();


    }


    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        try {
            int itemId = item.getItemId();

            if (itemId == R.id.nav_home) {
                loadFragment(new HomeFragment());
                return true;
            }

            // 个人资料页面不需要强制登录，因为已经处理了未登录状态
            if (itemId == R.id.nav_profile) {
                loadFragment(new ProfileFragment());
                return true;
            }

            // 其他需要登录的页面
            if (!isLoggedIn()) {
                showLoginDialog();
                return false;
            }

            if (itemId == R.id.nav_friends) {
                loadFragment(new FriendsFragment());
            } else if (itemId == R.id.nav_messages) {
                loadFragment(new MessagesFragment());
            }

            return true;

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "导航错误: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    private boolean isLoggedIn() {
        User currentUser = loginUser.getLoginUser();
        return currentUser != null && currentUser.getUsername() != null;
    }

    /**
     * // todo  获取后端数据
     * 获取推荐数据（目前是模拟数据，后期可替换为后端接口）
     */
    private void fetchRecommendations() {
        // 生成模拟数据 生成10个推荐用户
        UserUtils.generateUsers(10);
    }

    private void showLoginDialog() {
        new AlertDialog.Builder(this)
                .setTitle("需要登录")
                .setMessage("请先登录才能使用此功能")
                .setPositiveButton("登录", (dialog, which) -> {
                    startActivity(new Intent(this, LoginActivity.class));
                })
                .setNegativeButton("取消", null)
                .show();
    }


}